﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol.officer;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Formular;

namespace Pixysoft.Weblications.Sango.Command.officer
{
    partial class SangoOfficerHandler
    {
        public officer_hire officer_hire(string primarycode)
        {
            string userCode = SangoManager.Authentication();

            officer_hire response = ReflectionManager.CreatePojo<officer_hire>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);

            Entities.Usr_Officer officer = OfficerDao.getUserOfficer(userCode, primarycode);

            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);

            if (SettingHelper.BooleanValue(officer.Hired))
            {
                response.Message = "将领已经在职";
                return response;
            }

            if (property.Officercount <= property.Hiredofficercount)
            {
                response.Message = "可招聘名额已经满";
                return response;
            }


            Entities.Sys_Officer gOfficer = SysOfficerHelper.searchBySerialcode(officer.Officerserialcode);

            long cashprice = gOfficer.Recuitprice;

            if (!UsrProfileDao.tryDecreaseCashvalue(profile, cashprice))
            {
                response.Message = "银币不足，无法招募";
                return response;
            }

            OfficerDao.hireOfficer(officer, property);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                command.Update(officer);

                command.Update(profile);

                command.Update(property);

                command.Commit();
            });


            response.profile = UsrProfileFacade.ToProfile(profile);
            response.property = UsrProfileFacade.ToProperty(property);
            response.officer = OfficerFacade.ToOfficer(officer);
            response.Succeed = true;
            return response;
        }


    }
    public interface officer_hire : ISangoResponse
    {
        player_profile profile { get;set;}

        player_property property { get;set;}

        player_officer officer { get;set;}
    }
}
